import { Component, OnInit, Input, ViewChild, ElementRef, EventEmitter, Output } from '@angular/core';

@Component({
  selector: 'app-product-upload-image',
  templateUrl: './product-upload-image.component.html',
  styleUrls: ['./product-upload-image.component.css']
})
export class ProductUploadImageComponent implements OnInit {

  public _ref:any
  constructor() { }
  @Input() target: string = 'product/qi/';
  @Input() imagePath: string;
  @ViewChild("Img") imgInput: ElementRef;
  @Input() img:File // 双向绑定的img。File变量
  @Output() imgChange = new EventEmitter<File>();
  expressionError:boolean = false; // 是否要显示提示信息
  public getFormData():FormData | null{
    let imgFiles: [File] = this.imgInput.nativeElement.files;
    if (imgFiles[0] == void 0) {
      throw ("未选择图片，无法获取formData")
    }
    let formData = new FormData();
    formData.append("target", this.target);
    formData.append("image", imgFiles[0]);
    return formData
  }
  ngOnInit() {
  }
  remove(){
    this._ref.destory();
  }
  getImgFile():File|boolean{
    let imgFiles: [File] = this.imgInput.nativeElement.files;
    if (imgFiles[0] == void 0) {
      // throw ("未选择图片，无法获取formData")
      return false
    }
    return imgFiles[0]
  }
  change(){
    if(this.getImgFile()){
      this.expressionError = false
    }else{
      this.expressionError = true
    }
  }
}
